package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.neumooc.service.ColumnService;
import com.neusoft.neumooc.service.TemplateService;
import com.neusoft.neumooc.service.ContentTypeService;
import com.neusoft.neumooc.vo.ColumnVO;
import com.neusoft.neumooc.vo.TemplateVO;
import com.neusoft.neumooc.vo.ContentTypeVO;

@Controller
@RequestMapping(value = "/column")
public class ColumnController extends BaseController {

    @Autowired
    private ColumnService columnService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private ContentTypeService contentTypeService;

    /**
     * 跳转到栏目列表页面
     */
    @RequestMapping(value = "/list")
    public String list(Model model) {
        try {
            List<ColumnVO> columns = columnService.getAllColumns();
            model.addAttribute("columns", columns);
        } catch (Exception e) {
            log.error("获取栏目列表失败", e);
        }
        return "column/list";
    }
    
    /**
     * 跳转到添加栏目页面
     */
    @RequestMapping(value = "/add")
    public String add(Model model) {
        try {
            // 获取列表模板
            List<TemplateVO> listTemplates = templateService.getTemplatesByType("list");
            // 获取页面模板
            List<TemplateVO> pageTemplates = templateService.getTemplatesByType("page");
            // 获取内容类型列表
            List<ContentTypeVO> contentTypes = contentTypeService.getAllContentTypes();
            
            model.addAttribute("listTemplates", listTemplates);
            model.addAttribute("pageTemplates", pageTemplates);
            model.addAttribute("contentTypes", contentTypes);
        } catch (Exception e) {
            log.error("获取模板列表失败", e);
        }
        return "column/add";
    }
    
    /**
     * 跳转到编辑栏目页面
     */
    @RequestMapping(value = "/edit")
    public String edit(HttpServletRequest request, Model model) {
        try {
            String columnId = request.getParameter("columnId");
            log.info(" ======================== " + columnId);
            ColumnVO column = columnService.getColumnById(columnId);
            
            // 获取列表模板
            List<TemplateVO> listTemplates = templateService.getTemplatesByType("list");
            // 获取页面模板
            List<TemplateVO> pageTemplates = templateService.getTemplatesByType("page");
            // 获取内容类型列表
            List<ContentTypeVO> contentTypes = contentTypeService.getAllContentTypes();
            
            model.addAttribute("column", column);
            model.addAttribute("listTemplates", listTemplates);
            model.addAttribute("pageTemplates", pageTemplates);
            model.addAttribute("contentTypes", contentTypes);
        } catch (Exception e) {
            log.error("获取栏目失败", e);
        }
        return "column/edit";
    }
    
    /**
     * 添加栏目
     */
    @ResponseBody
    @RequestMapping(value = "/doAdd")
    public Map<String, Object> doAdd(ColumnVO column) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = columnService.addColumn(column);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "添加成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "添加失败");
            }
        } catch (Exception e) {
            log.error("添加栏目失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "添加栏目失败");
        }
        return retMap;
    }
    
    /**
     * 更新栏目
     */
    @ResponseBody
    @RequestMapping(value = "/update")
    public Map<String, Object> update(ColumnVO column) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = columnService.updateColumn(column);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "更新成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "更新失败");
            }
        } catch (Exception e) {
            log.error("更新栏目失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "更新栏目失败");
        }
        return retMap;
    }
    
    /**
     * 删除栏目
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public Map<String, Object> delete(@RequestParam("columnId") String columnId) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            boolean result = columnService.deleteColumn(columnId);
            if(result) {
                retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
                retMap.put(Params.RET_OBJ, "删除成功");
            } else {
                retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
                retMap.put(Params.RET_OBJ, "删除失败");
            }
        } catch (Exception e) {
            log.error("删除栏目失败", e);
            retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
            retMap.put(Params.RET_OBJ, "删除栏目失败");
        }
        return retMap;
    }
} 